package br.com.MOS.Applications.Exemplos.HelloLWUIT;

import com.sun.dtv.lwuit.Button;
import com.sun.dtv.lwuit.Form;
import com.sun.dtv.lwuit.events.ActionEvent;
import com.sun.dtv.lwuit.events.ActionListener;
import com.sun.dtv.lwuit.Label;
import com.sun.dtv.lwuit.layouts.CoordinateLayout;
import com.sun.dtv.lwuit.plaf.UIManager;
import com.sun.dtv.resources.TimeoutException;
import javax.microedition.xlet.Xlet;
import javax.microedition.xlet.XletContext;
import javax.microedition.xlet.XletStateChangeException;
import java.util.Hashtable;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * HelloLWUIT.java
 *
 * @author Rafael / Philippe / Leonardo Baptista (cbs)
 */
public class HelloLWUIT implements Xlet {
    private static XletContext context;
    com.sun.dtv.ui.Screen s;

    /**
     * Esse metodo e necessario para o NetBeans identificar a classe principal no manifest
     * @param args
     */
    public static void main(String[] args) {
        // TODO code application logic here

    }

    /**
     * Start()
     */
    public void start() {
        init();
        setupTheme();
        Form mainForm = buildUI();
        try {
            s.reserve(true, 0L, null);
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(HelloLWUIT.class.getName()).log(Level.SEVERE, null, ex);
            exit();
        } catch (TimeoutException ex) {
            Logger.getLogger(HelloLWUIT.class.getName()).log(Level.SEVERE, null, ex);
            exit();
        } catch (SecurityException ex) {
            Logger.getLogger(HelloLWUIT.class.getName()).log(Level.SEVERE, null, ex);
            exit();
        }
        mainForm.show();
    }

    /**
     * init()
     */
    public void init() 
    {
        s = com.sun.dtv.ui.Device.getInstance().getScreens()[0];
    }

    public void setupTheme() {
     // Theme
        Hashtable themeMap = new Hashtable();
        themeMap.put("bgColor", "000000");
        themeMap.put("fgColor", "FFFFFF");
        themeMap.put("Button.bgColor", "000000");
        themeMap.put("Button.fgColor", "FFFFFF");
        themeMap.put("Label.bgColor", "113663");
        themeMap.put("Label.fgColor", "FFFFFF");
        themeMap.put("Label.bgSelColor", "113663");
        themeMap.put("Label.fgSelColor", "FFFFFF");
        themeMap.put("TextArea.bgColor", "000000");
        themeMap.put("TextArea.fgColor", "FFFFFF");
        UIManager.getInstance().setThemeProps(themeMap);
    }

    /**
     * exit()
     */
    public void exit() 
    {
        context.notifyDestroyed();
    }

    /**
     * Cria o form
     * @return
     */
    public Form buildUI() {

        Form mainForm = new Form("Hello LWUIT");
        mainForm.setLayout(new CoordinateLayout(300, 300));
        mainForm.setX(0);
        mainForm.setY(0);
        mainForm.setWidth(300);
        mainForm.setHeight(300);
        //Sair
        final Button btnSair = new Button("Sair");
        btnSair.setX(0);
        btnSair.setY(22);
        // Create buttons in the main form
        final Label lblHello = new Label("Hello LWUIT");

        ActionListener listener = new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                    if (evt.getSource() == btnSair) {
                    exit();
                }


            }
        };

        btnSair.addActionListener(listener);
        mainForm.addComponent(lblHello);
        mainForm.addComponent(btnSair);
        mainForm.setCommandListener(listener);

        return mainForm;
    }

    /**
     * initXlet()
     *
     * @param ctx Xlet Context
     * @throws javax.microedition.xlet.XletStateChangeException
     */
    public void initXlet(XletContext ctx) throws XletStateChangeException {
        context = ctx;
    }

    /**
     * startXlet()
     *
     * @throws javax.microedition.xlet.XletStateChangeException
     */
    public void startXlet() throws XletStateChangeException {
        HelloLWUIT a = new HelloLWUIT();
        a.start();
    }

    /**
     * pauseXlet()
     */
    public void pauseXlet() {

    }

    /**
     * destroyXlet()
     *
     * @param unconditional
     * @throws javax.microedition.xlet.XletStateChangeException
     */
    public void destroyXlet(boolean unconditional) throws XletStateChangeException {

    }

}